﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EF4.Core.IoC;
using System.Web.Routing;

namespace EF4.UI.MVC.Controllers
{
    public class IoCControllerFactory : DefaultControllerFactory
    {
        
        protected override IController GetControllerInstance(RequestContext requestContext,Type controllerType)
        {
            try
            {
                if (controllerType == null)
                    throw new ArgumentNullException("controllerType");

                if (!typeof(IController).IsAssignableFrom(controllerType))
                    throw new ArgumentException(string.Format(
                        "Type requested is not a controller: {0}",
                        controllerType.Name),
                        "controllerType");
                return IoCWorker.Resolve(controllerType) as IController;
            }
            catch (Exception ex)
            {

                return base.GetControllerInstance(requestContext,controllerType);
            }
            return null;

        }
    }
}